package com.zhiyiang.service.room;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyiang.Vo.OrderVO;
import com.zhiyiang.Vo.RoomVO;
import com.zhiyiang.mapper.HotelMapper;
import com.zhiyiang.mapper.RoomMapper;
import com.zhiyiang.mapper.TypeMapper;
import com.zhiyiang.mapper.UserMapper;
import com.zhiyiang.pojo.Hotel;
import com.zhiyiang.pojo.Room;
import com.zhiyiang.pojo.Type;
import com.zhiyiang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private TypeMapper typeMapper;
    @Override
    public List<Room> findAll() {
        List<Room> rooms = roomMapper.selectList(null);
        return rooms;
    }

    @Override
    public Page<RoomVO> getAllHotel(Integer current, Integer size) {
        Page<Room> page = new Page<>(current, size);
        Page<Room> roomPage = roomMapper.selectPage(page, null);
        List<RoomVO> records= new ArrayList<>();
        for (Room room:roomPage.getRecords()) {
            RoomVO roomVO = new RoomVO();
            roomVO.setId(room.getId());
            roomVO.setRoomcode(room.getRoomcode());
            roomVO.setRoomstate(room.getRoomstate());
            roomVO.setRoomdec(room.getRoomdec());
            QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", room.getRoomtype());
            Type type = typeMapper.selectOne(queryWrapper);
            roomVO.setRoomtype(type.getTypename());
            QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", room.getHotelid());
            Hotel hotel = hotelMapper.selectOne(queryWrapper1);
            roomVO.setHotelid(hotel.getHotelname());
            records.add(roomVO);
        }
        Page<RoomVO> roomVOPage = new Page<RoomVO>(roomPage.getCurrent(), roomPage.getSize(), roomPage.getTotal());
        roomVOPage.setPages(roomPage.getPages());
        roomVOPage.setOrders(roomPage.getOrders());
        roomVOPage.setCountId(roomPage.getCountId());
        roomVOPage.setMaxLimit(roomPage.getMaxLimit());
        roomVOPage.setRecords(records);

        return roomVOPage;
    }

    @Override
    public Page<RoomVO> getRoom(String usercode,String query, Integer current, Integer size) {
        Page<Room> page = new Page<>(current, size);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userid", user.getId());
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("roomcode", query).eq("hotelid", hotel.getId());
        Page<Room> roomPage = roomMapper.selectPage(page, queryWrapper2);


        List<RoomVO> records= new ArrayList<>();
        for (Room room:roomPage.getRecords()) {
            RoomVO roomVO = new RoomVO();
            roomVO.setId(room.getId());
            roomVO.setRoomcode(room.getRoomcode());
            roomVO.setRoomstate(room.getRoomstate());
            roomVO.setRoomdec(room.getRoomdec());
            QueryWrapper<Type> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("id", room.getRoomtype());
            Type type = typeMapper.selectOne(queryWrapper3);
            roomVO.setRoomtype(type.getTypename());
            QueryWrapper<Hotel> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("id", room.getHotelid());
            Hotel hotel2 = hotelMapper.selectOne(queryWrapper4);
            roomVO.setHotelid(hotel2.getHotelname());
            records.add(roomVO);


        }
        Page<RoomVO> roomVOPage = new Page<RoomVO>(roomPage.getCurrent(), roomPage.getSize(), roomPage.getTotal());
        roomVOPage.setPages(roomPage.getPages());
        roomVOPage.setOrders(roomPage.getOrders());
        roomVOPage.setCountId(roomPage.getCountId());
        roomVOPage.setMaxLimit(roomPage.getMaxLimit());
        roomVOPage.setRecords(records);
        return roomVOPage;
    }

    @Override
    public Boolean deleteRoom(String id) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(id), "id", id);
        int delete = roomMapper.delete(queryWrapper);
        Boolean flag = false;
        if (delete>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean addRoom(Room room) {
        Boolean flag = false;
        int insert = roomMapper.insert(room);
        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Room getRoom(Integer id) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(String.valueOf(id)),"id", id);
        Room room = roomMapper.selectOne(queryWrapper);
        return room;
    }

    @Override
    public Boolean updateRoom(String id, Room room) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(id),"id", id);
        int update = roomMapper.update(room,queryWrapper);
        boolean flag = false;
        if (update>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Page<RoomVO> getLoginRoom(String usercode, Integer current, Integer size) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userid", user.getId());
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("hotelid", hotel.getId());
        Page<Room> page = new Page<>(current,size);
        Page<Room> roomPage = roomMapper.selectPage(page, queryWrapper2);


        List<RoomVO> records= new ArrayList<>();
        for (Room room:roomPage.getRecords()) {
            RoomVO roomVO = new RoomVO();
            roomVO.setId(room.getId());
            roomVO.setRoomcode(room.getRoomcode());
            roomVO.setRoomstate(room.getRoomstate());
            roomVO.setRoomdec(room.getRoomdec());
            QueryWrapper<Type> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("id", room.getRoomtype());
            Type type = typeMapper.selectOne(queryWrapper3);
            roomVO.setRoomtype(type.getTypename());
            QueryWrapper<Hotel> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("id", room.getHotelid());
            Hotel hotel2 = hotelMapper.selectOne(queryWrapper4);
            roomVO.setHotelid(hotel2.getHotelname());
            records.add(roomVO);


        }
        Page<RoomVO> roomVOPage = new Page<RoomVO>(roomPage.getCurrent(), roomPage.getSize(), roomPage.getTotal());
        roomVOPage.setPages(roomPage.getPages());
        roomVOPage.setOrders(roomPage.getOrders());
        roomVOPage.setCountId(roomPage.getCountId());
        roomVOPage.setMaxLimit(roomPage.getMaxLimit());
        roomVOPage.setRecords(records);
        return roomVOPage;
    }

    @Override
    public void updateState(Integer id) {
        Room room = new Room();
        room.setId(id);
        room.setRoomstate(0);
        roomMapper.updateById(room);
    }

    @Override
    public List<Room> getRooms(Integer orderhotel,Integer roomtype) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomtype", roomtype).eq("hotelid", orderhotel).in("roomstate", 0, 5);
        List<Room> rooms = roomMapper.selectList(queryWrapper);
        return rooms;
    }

    @Override
    public void reseveState(Integer roomcode,Integer orderhotel) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomcode", roomcode).eq("hotelid", orderhotel);
        Room room = roomMapper.selectOne(queryWrapper);
        room.setRoomstate(4);

    }
}
